/*
 * MTSApp.cpp
 *
 *  Created on: Oct 19, 2012
 *      Author: johnny
 */

#include <iostream>
#include <boost/program_options.hpp>
#include <boost/format.hpp>
#include <boost/thread.hpp>

#include <QApplication>

#include "MeasWorld.h"
#include "Logger.h"
#include "ConsoleListener.h"
#include "gui/MainWindow.h"

int  startUI(int& argc, char **argv);
void startConsole(std::string cfg, std::string rec);
void readIni();

int main(int argc, char **argv) {
	namespace po = boost::program_options;

    std::string configFile, recFile;

	po::options_description descr("Allowed options");
	descr.add_options()
			("help,h", "print this usage help")
            ("config",  po::value<std::string>(), "use this configuration")
            ("rec",     po::value<std::string>(), "use this recording")
            ("no-gui",  po::value<std::string>(), "do not start a user interface")
            ("measdir", po::value<std::string>(), "use this measurement folder")
            ("datadir", po::value<std::string>(), "use this data folder")
            ("cfgdir",  po::value<std::string>(), "use this configuration folder")
            ("modir",   po::value<std::string>(), "use this measurement objects folder")
	;

    readIni();

	po::variables_map vm;

    try {
        po::store(po::parse_command_line(argc, argv, descr), vm);
        po::notify(vm);

        if (vm.count("help")) {
            std::cout << descr << std::endl;
            return 0;
        }
        if(vm.count("measdir")) {
            util::setDir(util::MeasDir, vm["measdir"].as<std::string>());
        }
        if(vm.count("datadir")) {
            util::setDir(util::DataDir, vm["datadir"].as<std::string>());
        }
        if(vm.count("cfgdir")) {
            util::setDir(util::CfgDir, vm["cfgdir"].as<std::string>());
        }
        if(vm.count("modir")) {
            util::setDir(util::MoDir, vm["modir"].as<std::string>());
        }
        if (vm.count("no-gui")) {
            if (vm.count("config")) {
                configFile = vm["config"].as<std::string>();
            }
            if (vm.count("rec")) {
                recFile = vm["rec"].as<std::string>();
            }
            startConsole(configFile, recFile);
        } else {
            return startUI(argc, argv);
        }
    } catch(po::unknown_option& ex) {
        std::cout << ex.what() << std::endl;
    }

	return 0;
}

int startUI(int& argc, char **argv) {
    core::MeasWorld& world = core::MeasWorld::getInstance();
    world.setup();

	QApplication app(argc, argv);
	ui::MainWindow mainWindow;

	if(mainWindow.setup())
		return app.exec();

	std::cerr << "Error on main window setup\n";
	return 1;
}

void startConsole(std::string cfg, std::string rec) {
	using namespace core;

    MeasWorld& measWorld = MeasWorld::getInstance();
	measWorld.setup();

	user::ConsoleListener console(measWorld);

	if (!cfg.empty()) {
		if (ConfigurationManager::getInstance().loadCfg(cfg)) {
			if(!rec.empty()) {
				if(!measWorld.loadRec(rec)) {
					Logger::getInstance().add(
                                Warning, (boost::format("Cannot load rec %1%") % rec).str(), L"MTS");
				}
			}
		}
	}

	console.listen();
}

void readIni() {
#ifdef WIN32
    util::setDir(util::MeasDir, std::wstring(L"C:\\mtsv3"));
#else
    util::setDir(util::MeasDir, std::wstring(L"/home/johnny/measurementDir"));
#endif
}
